<?php

namespace App\Http\Controllers\Api\AdminCode;

use App\Wen\Utils\Utils;
use App\Http\Controllers\Api\AdminCode\Traits\adminRequestUserData;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\Repositories\WxCollectRepositores;
use App\Http\Controllers\Api\Repositories\WxCommentLikeRepositores;
use App\Http\Controllers\Api\Repositories\WxLikeRepositores;
use App\Http\Controllers\Api\Repositories\WxSyncRelationRepositories;
use App\Http\Controllers\Api\Repositories\WxUserFollowRepositores;
use App\Http\Controllers\Api\Repositories\WxUserRepositores;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Models\User\WxUser;
use Illuminate\Http\Request;

class AdminActionsController extends BaseController
{
    use adminRequestUserData;
    /**
     * 增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batch(Request $request){

        if(_empty_($request->actions)){
            return $this->fail(1001006, []);
        }
        $success_actions = [];
        $fail_actions = [];
        $todo_actions = [];
        foreach ($request->actions as $action){
            $action['author']['nickName'] = trim($action['author']['nickName'] ?? '');
            // 检查用户是否存在
            if(_empty_($action['author']) || _empty_($action['author']['id']) || _empty_($action['author']['nickName'])){
                continue;
            }
            if(_empty_($action['id'])){
                continue;
            }
            $uid = WxSyncRelationRepositories::wx_uid($request->domain, $action['author']['id']);
            if(!$uid){
                $data = [];
                $data['domain'] = $request->domain;
                $data['mp_openId'] = $action['author']['mp_openId'] ?? '';
                $data['web_openId'] = $action['author']['web_openId'] ?? '';
                $data['app_openId'] = $action['author']['app_openId'] ?? '';
                $data['unionId'] = $action['author']['unionId'] ?: '';
                $data['nickName'] = $action['author']['nickName'];
                $data['user_background_maps'] = $action['author']['user_background_maps'] ?: '';
                $data['avatarUrl'] = $action['author']['avatarUrl'] ?: '';
                $data['gender'] = $action['author']['gender'] ?: 0;
                $data['external_id'] = $action['author']['id'];
                $data['date'] = $action['author']['date'];
                $uid = WxUserRepositores::registerUser($data);
                if(!$uid){
                    return $this->fail(200002, ['msg'=>'用户数据插入失败']);
                }
            }
            sleep(1);

            $r = null;
            if($action['type'] == 'like'){
                $postId = WxSyncRelationRepositories::wx_post_id($request->domain, $action['object']);
                if($postId){
                    $r = WxLikeRepositores::admin_like($uid, $postId);
                }else{
                    $todo_actions[] = ['type'=>'post', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'unlike'){
                $postId = WxSyncRelationRepositories::wx_post_id($request->domain, $action['object']);
                if($postId) {
                    $r = WxLikeRepositores::unlike($uid, $postId);
                }else{
                    $todo_actions[] = ['type'=>'post', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'collectPost'){
                $postId = WxSyncRelationRepositories::wx_post_id($request->domain, $action['object']);
                if($postId) {
                    $r = WxCollectRepositores::collect($uid, $postId);
                }else{
                    $todo_actions[] = ['type'=>'post', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'unCollectPost'){
                $postId = WxSyncRelationRepositories::wx_post_id($request->domain, $action['object']);
                if($postId) {
                    $r = WxCollectRepositores::uncollect($uid, $postId);
                }else{
                    $todo_actions[] = ['type'=>'post', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'commentUp'){
                $commentId = WxSyncRelationRepositories::wx_comment_id($request->domain, $action['object']);
                if($commentId) {
                    $r = WxCommentLikeRepositores::like($uid, $commentId);
                }else{
                    $todo_actions[] = ['type'=>'comment', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'unCommentUp'){
                $commentId = WxSyncRelationRepositories::wx_comment_id($request->domain, $action['object']);
                if($commentId) {
                    $r = WxCommentLikeRepositores::unlike($uid, $commentId);
                }else{
                    $todo_actions[] = ['type'=>'comment', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'followUser'){
                $uid = WxSyncRelationRepositories::wx_uid($request->domain, $action['object']);
                if($uid) {
                    $r = WxUserFollowRepositores::follow($uid, $uid);
                }else{
                    $todo_actions[] = ['type'=>'user', 'object'=>$action['object']];
                }
            }else if($action['type'] == 'unfollowUser'){
                $uid = WxSyncRelationRepositories::wx_uid($request->domain, $action['object']);
                if($uid) {
                    $r = WxUserFollowRepositores::unfollow($uid, $uid);
                }else{
                    $todo_actions[] = ['type'=>'user', 'object'=>$action['object']];
                }
            }

            if($r){
                $success_actions[] = $action['id'];
            }else{
                $fail_actions[] = $action['id'];
            }
        }
        return $this->success(['success'=>$success_actions, 'fail'=>$fail_actions, 'todo'=>$todo_actions]);
    }

    /**
     * 新增用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function user(Request $request){
        // 检查用户是否存在
        if(_empty_($request->author) || _empty_($request->author['id']) || _empty_($request->author['nickName'])){
            return $this->fail(1001001, []);
        }

        $uid = WxSyncRelationRepositories::wx_uid($request->domain, $request->author['id']);
        if(_empty_($uid)){
            $data = $this->collectUserData($request);
            $uid = WxUserRepositores::registerUser($data);
            if(!$uid){
                return $this->fail(200002, ['msg'=>'用户数据插入失败']);
            }
        }

        return $this->success(['msg'=>'ok', 'uid'=>$uid]);

    }

}
